package com.nandi.deviceinstall.presenter

import com.nandi.deviceinstall.base.BasePresenter
import com.nandi.deviceinstall.bean.BaseObjectBean
import com.nandi.deviceinstall.bean.LoginBean
import com.nandi.deviceinstall.bean.TapRecordInfoBean
import com.nandi.deviceinstall.contract.*
import com.nandi.deviceinstall.model.*
import com.nandi.deviceinstall.net.BaseSubscriber
import com.nandi.deviceinstall.net.RxScheduler
import com.uber.autodispose.FlowableSubscribeProxy
import okhttp3.ResponseBody
import org.reactivestreams.Subscription


class TapRecordInfoPresenter : BasePresenter<TapRecordInfoContract.View>(), TapRecordInfoContract.Presenter {

    private val model: TapRecordInfoContract.Model

    init {
        model = TapRecordInfoModel()
    }
    override fun requestTapRecordInfo(token: String, id: Int) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeTapRecordInfo(token,id)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<TapRecordInfoBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<TapRecordInfoBean>(mView!!) {
                    override fun onNext(t: TapRecordInfoBean) {
                        mView!!.handlerTapRecordResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }


    override fun requestDownPic(token: String, path: String) {
        var fileName = path.substring(path.lastIndexOf("/")+1)
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeDownPic(token,path)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<ResponseBody>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<ResponseBody>(mView!!) {
                    override fun onNext(t: ResponseBody) {
                        //mView!!.uploadPicSuccess(t)
                        mView!!.downPicSuccess(t,fileName)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }
}

